package com.cn.sc.scitc.cyol;


import org.json.JSONObject;
import com.google.gson.JsonArray;
import com.google.gson.JsonIOException;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.google.gson.JsonSyntaxException;

import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;

public class Search {
    private String MyJson;
    public String reStr;
    public void stu(String name,String Token){
        JSONObject jsonParam = new JSONObject();
        jsonParam.put("orgId", "505695");
        jsonParam.put("stagesId", "98");
        jsonParam.put("name", name);
        jsonParam.put("tel", "");
        jsonParam.put("pageNo", "1");
        jsonParam.put("pageSize", "10");

        String urls ="http://dxx.scyol.com/backend/study/student/list";

        MyJson=GetJson(urls,jsonParam,Token);
        ReJson();
    }

    public String GetJson(String urls,JSONObject jsonObject,String Token) {
        StringBuffer sb=new StringBuffer();
        try {
            URL url = new URL(urls);// 创建url资源
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();// 建立http连接
            conn.setDoOutput(true);// 设置允许输出
            conn.setDoInput(true);// 设置允许输入
            conn.setUseCaches(false);// 设置不用缓存
            conn.setRequestMethod("POST");// 设置传递方式
            conn.setRequestProperty("Connection", "Keep-Alive");// 设置维持长连接
            conn.setRequestProperty("token", Token);// 设置维持长连接
            conn.setRequestProperty("Content-Type", "application/json;charset=UTF-8");// 设置文件字符集:
            byte[] data = (jsonObject.toString()).getBytes();// 转换为字节数组
            conn.setRequestProperty("Content-Length", String.valueOf(data.length));// 设置文件长度
            conn.setRequestProperty("contentType", "application/json");// 设置文件类型:
            conn.connect();// 开始连接请求
            OutputStream out = new DataOutputStream(conn.getOutputStream()) ;
            out.write((jsonObject.toString()).getBytes());// 写入请求的字符串
            out.flush();
            out.close();
            System.out.println("青年大学习：" + conn.getResponseCode());
            // 请求返回的状态
            if (HttpURLConnection.HTTP_OK == conn.getResponseCode()){
                System.out.println("青年大学习：连接成功");
                InputStream in1 = conn.getInputStream();// 请求返回的数据
                try {
                    String readLine=new String();
                    BufferedReader responseReader=new BufferedReader(new InputStreamReader(in1,"UTF-8"));
                    while((readLine=responseReader.readLine())!=null){
                        sb.append(readLine).append("");//Json 结束尾巴
                    }
                    responseReader.close();
                    System.out.println("青年大学习_Json获取:" + sb.toString());

                } catch (Exception e1) {
                    e1.printStackTrace();
                }
            } else {
                System.out.println("error++");
            }
        } catch (Exception e) {
        }
        return sb.toString();
    }


    public void ReJson(){
        JsonParser parse =new JsonParser();  //创建json解析器
        StringBuffer ts = new StringBuffer();
        try {
            JsonObject json=(JsonObject) parse.parse(MyJson.toString());  //创建jsonObject对象
            JsonArray array = json.get("data").getAsJsonArray();
            if(array.size()>1){
                ts.append("青年大学习完成情况如下:\n");
                for(int i=0;i<array.size();i++){
                    JsonObject subObject=array.get(i).getAsJsonObject();
                    ts.append(subObject.get("name").getAsString());
                    if(i<array.size()-1){
                        ts.append("\n");
                    }
                }
            }else if(array.size()==0){
                ts.append("查询失败，后台数据不存在");
            }else if(array.size()==1){
                ts.append("学习详细查询\n");
                for(int i=0;i<array.size();i++){
                    JsonObject subObject=array.get(i).getAsJsonObject();
                    ts.append("姓名："+subObject.get("name").getAsString()+"\n");
                    ts.append("所属："+subObject.get("allOrgName").getAsString()+"\n");
                    ts.append("学习时间："+subObject.get("startTimeStr").getAsString()+"\n");
                    ts.append("电话："+subObject.get("tel").getAsString()+"");
                }
            }
            reStr = ts.toString();
        } catch (JsonIOException e) {
            e.printStackTrace();
        } catch (JsonSyntaxException e) {
            e.printStackTrace();
        }
    }

    public String getReStr() {
        return reStr;
    }
}
